package com.bestcem.xm.ticket.grpc.client;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.survey.grpc.v1.services.*;
import com.bestcem.xm.ticket.grpc.client.convert.TicketBaseSurveyConvert;
import com.bestcem.xm.ticket.grpc.client.dto.survey.*;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import zhongyan.xm.base.v1.Base;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

/**
 * 倍市得 Survey 模块相关rpc接口客户端
 *
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/27 14:04
 */
@Slf4j
@Service
public class TicketBaseSurveyGrpcService {

    @GrpcClient(GrpcClientNames.DS_SURVEY)
    private BaseSurveyServiceGrpc.BaseSurveyServiceBlockingStub baseSurveyServiceBlockingStub;

    /**
     * 根据id获取单行数据
     *
     * @param id 答卷数据显示列id
     * @return 单行数据
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/27 14:38
     */
    @Nullable
    public ColumnDTO getColumn(String id) {
        GetColumnRequest.Builder builder = GetColumnRequest.newBuilder();
        if (StringUtils.isNotEmpty(id)) {
            builder.setId(id);
        }
        try {
            ColumnResponse response = baseSurveyServiceBlockingStub.getColumn(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] Survey getColumn Error; SurveyId: {} ErrorMsg: {}", id, response);
                return null;
            }
            return TicketBaseSurveyConvert.responseToColumnDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] Survey getColumn Error; SurveyId: {}", id, e);
            return null;
        }

    }

    /**
     * 通过查询条件批量一次性获取所有符合条件的Respondent
     *
     * @param respondent 终端
     * @param search     可供搜索的字段,若空则不可搜索
     * @param status     若空则传空数组
     * @param fields     此参数设置后只返回指定的字段
     * @return Respondent列表
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/30 11:41
     */
    @Nullable
    public List<RespondentDTO> listRespondent(Respondent respondent, String search, List<Integer> status, List<String> fields) {
        ListRespondentRequest.Builder builder = ListRespondentRequest.newBuilder();
        if (Objects.nonNull(respondent)) {
            builder.setRespondent(respondent);
        }
        if (!ObjectUtils.isEmpty(search)) {
            builder.setSearch(search);
        }
        if (!CollectionUtils.isEmpty(status)) {
            builder.addAllStatus(status);
        }
        if (!CollectionUtils.isEmpty(fields)) {
            builder.addAllFields(fields);
        }
        try {
            RespondentSearchResponse response = baseSurveyServiceBlockingStub.listRespondent(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] Survey listRespondent Error; RespondentId: {} ErrorMsg: {}", respondent.getId(), response);
                return null;
            }
            return TicketBaseSurveyConvert.responseToRespondentDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] Survey listRespondent Error; RespondentId: {}", respondent.getId(), e);
            return null;
        }

    }

    /**
     * 根据id获取单个配额检查项
     *
     * @param quotaCheckItemId 配额检查项id
     * @return 配额检查项DTO
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/30 11:27
     */
    @Nullable
    public QuotaCheckItemDTO getQuotaCheckItem(String quotaCheckItemId) {
        GetQuotaCheckItemRequest.Builder builder = GetQuotaCheckItemRequest.newBuilder();
        if (StringUtils.isNotEmpty(quotaCheckItemId)) {
            builder.setId(quotaCheckItemId);
        }
        try {
            QuotaCheckItemResponse response = baseSurveyServiceBlockingStub.getQuotaCheckItem(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] Survey getQuotacheckitem Error; quotaCheckItemId: {} ErrorMsg: {}", quotaCheckItemId, response);
                return null;
            }
            return TicketBaseSurveyConvert.responseToQuotaCheckItemDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] Survey getQuotacheckitem Error; quotaCheckItemId: {}", quotaCheckItemId, e);
            return null;
        }

    }

    /**
     * 根据id获取单个QuotaCondition
     *
     * @param quotaConditionId 配额条件id
     * @return QuotaConditionDTO 配额条件详情
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/30 11:40
     */
    @Nullable
    public QuotaConditionDTO getQuotaCondition(String quotaConditionId) {
        GetQuotaConditionRequest.Builder builder = GetQuotaConditionRequest.newBuilder();
        if (StringUtils.isNotEmpty(quotaConditionId)) {
            builder.setId(quotaConditionId);
        }

        try {
            QuotaConditionResponse response = baseSurveyServiceBlockingStub.getQuotaCondition(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] Survey getQuotacondition Error; quotaConditionId: {} ErrorMsg: {}", quotaConditionId, response);
                return null;
            }
            return TicketBaseSurveyConvert.responseToQuotaConditionDTO(response);
        } catch (Exception e) {
            log.error("[Ticket] Survey getQuotacondition Error; quotaConditionId: {}", quotaConditionId, e);
            return null;
        }
    }

    /**
     * 根据id获取单个 respondentData
     *
     * @param id respondentDataId
     * @return QuotaConditionDTO 配额条件详情
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/30 11:40
     */
    @Nullable
    public RespondentDataDTO getRespondentData(String id) {
        if (!ObjectId.isValid(id)) {
            return null;
        }
        GetRespondentDataRequest.Builder builder = GetRespondentDataRequest.newBuilder();
        builder.setId(id);
        try {
            RespondentDataResponse response = baseSurveyServiceBlockingStub.getRespondentData(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] Survey getRespondentData Error; {} ErrorMsg: {}", id, response);
                return null;
            }
            return TicketBaseSurveyConvert.infoToRespondentDataDTO(response.getRespondentData());
        } catch (Exception e) {
            log.error("[Ticket] Survey getRespondentData Error; {}", id, e);
            return null;
        }
    }
}
